package com.bii.gelapp;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class GaussianActivity extends Activity {
	String analysisorigin = "";
	ImageView ivImage;
	public Bitmap originalBmp; //origin
	public Bitmap activeBmp; //edited image
	Button btPrev, btGabor, btSave;
	TextView tvBlur;
	SeekBar sbBlur;
	static int currentprogress;
	int gaussiansigma = 0;
	int gaussiansigma2 = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gaussianactivity);
		
		if (getIntent().hasExtra("analysisorigin")) {
			analysisorigin = getIntent().getStringExtra("analysisorigin");
		}
		
		ivImage = (ImageView)findViewById(R.id.ivImage);
		//byte[] byteArray = getIntent().getByteArrayExtra("BMP");
		//originalBmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
		try {
			originalBmp = InternalStorage.readBitmap(this, "rotatedBmp");
			//Bitmap resizedbmp = Bitmap.createScaledBitmap(originalBmp, 300,
			//		300, true);
			//ivImage.setImageBitmap(resizedbmp);
			ivImage.setImageBitmap(originalBmp);
			
			btPrev = (Button)findViewById(R.id.btPrev);
			btGabor = (Button)findViewById(R.id.btGabor);
			btSave = (Button)findViewById(R.id.btSave);
			tvBlur = (TextView)findViewById(R.id.tvBlur);
			sbBlur = (SeekBar)findViewById(R.id.sbBlur);
			sbBlur.incrementProgressBy(0);
			sbBlur.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				
				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onProgressChanged(SeekBar seekBar, int progress,
						boolean fromUser) {
					currentprogress = sbBlur.getProgress();
					gaussiansigma = currentprogress;
					gaussiansigma2 = currentprogress;
					if (currentprogress != 0) {
						//get original image and convert to mat
						Mat srcImg = new Mat(originalBmp.getHeight(), originalBmp
								.getWidth(), CvType.CV_8U, new Scalar(4));
						Bitmap myBitmap32 = originalBmp.copy(Bitmap.Config.ARGB_8888,
								true);
						Utils.bitmapToMat(myBitmap32, srcImg);
						//apply gaussian blur
						Mat noiseFilteredImg = new Mat();
						Imgproc.GaussianBlur(srcImg, noiseFilteredImg, new Size(33,
								33), gaussiansigma, gaussiansigma2);
						Bitmap gaussianBlurredBmp = Bitmap.createBitmap(noiseFilteredImg.cols(),
								noiseFilteredImg.rows(), Bitmap.Config.RGB_565);
						Utils.matToBitmap(noiseFilteredImg, gaussianBlurredBmp);
						//set activeBmp
						activeBmp= gaussianBlurredBmp;
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(gaussianBlurredBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						ivImage.setImageBitmap(activeBmp);
						tvBlur.setText("Gaussian blur: " + currentprogress);
						
						
					}
					else
					{
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(originalBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						//ivImage.setImageBitmap(activeBmp);
						ivImage.setImageBitmap(originalBmp);
						tvBlur.setText("Gaussian blur: " + currentprogress);
					}
					
				}
				
				
			});
			
			btPrev.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					startActivity(new Intent(GaussianActivity.this, RotationActivity.class));
					
				}
			});
			
			btGabor.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
					Intent goToNextActivity = new Intent(getApplicationContext(),
							GaborActivity2.class);
					goToNextActivity.putExtra("analysisorigin", analysisorigin);
					
					byte[] bytes;
					if(activeBmp == null){
						try {
							InternalStorage.writeBitmap(GaussianActivity.this, "gaussianBmp", originalBmp);
							startActivity(goToNextActivity);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else
					{
						try {
							InternalStorage.writeBitmap(GaussianActivity.this, "gaussianBmp", activeBmp);
							startActivity(goToNextActivity);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					/*
					if(analysisorigin.equals("Protein")){
					//Intent goToNextActivity = new Intent(getApplicationContext(),
					//		GaborActivity.class);
					Intent goToNextActivity = new Intent(getApplicationContext(),
							GaborActivity2.class);
					goToNextActivity.putExtra("analysisorigin", analysisorigin);
					
					byte[] bytes;
					if(activeBmp == null){
						try {
							InternalStorage.writeBitmap(GaussianActivity.this, "gaussianBmp", originalBmp);
							startActivity(goToNextActivity);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else
					{
						
						try {
							InternalStorage.writeBitmap(GaussianActivity.this, "gaussianBmp", activeBmp);
							startActivity(goToNextActivity);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
					}
					
					
					//goToNextActivity.putExtra("BMP", bytes);
					//startActivity(goToNextActivity);
					}
					else
					{
						Intent goToNextActivity = new Intent(getApplicationContext(),
								DNAGaborActivity.class);
						goToNextActivity.putExtra("analysisorigin", analysisorigin);
						byte[] bytes;
						if(activeBmp == null){
							try {
								InternalStorage.writeBitmap(GaussianActivity.this, "gaussianBmp", originalBmp);
								startActivity(goToNextActivity);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						else
						{
							try {
								InternalStorage.writeBitmap(GaussianActivity.this, "gaussianBmp", activeBmp);
								startActivity(goToNextActivity);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						
					}
					*/
					
				}
			});
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
	
	public byte[] compressImage(Bitmap image)
	{
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.PNG, 0, stream);
		byte[] bytes = stream.toByteArray();
		return bytes;
		
	}
	
	
}
